package com.huitone.gddw.service.impl.monitoring;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.entity.UserGate;
import com.huitone.gddw.dao.monitoring.IUserGateDao;
import com.huitone.gddw.service.monitoring.IUserGateSerivce;
import com.huitone.gddw.utils.StringUtils;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 用户通道表
 * @author wuluodan
 *
 */
@Service
public class UserGateSerivce implements IUserGateSerivce {

	@Autowired
	private IUserGateDao userGateDao;

	@Override
	public List<UserGate> find(Map<String, Object> params) {
		return userGateDao.findGateByUser(params.get("userId").toString());
	}

	@Override
	public void delete(Map<String, Object> params) {
		userGateDao.deleteByUserId(params);
	}

	@Override
	public void updateGateLinkId(String userId, List<String> listSave, List<String> listDel) {
		List<UserGate> list = new ArrayList<>();
		for (String gateLinkId : listSave) {
			UserGate userGate = new UserGate();
			userGate.setUserId(userId);
			userGate.setGateLinkId(gateLinkId);
			list.add(userGate);
		}
		this.saveBatch(list);
		this.deleteBatch(userId, listDel);
	}

	/**
	 * 批量保存
	 * @param userGates
	 */
	private void saveBatch(List<UserGate> userGates) {
		if (userGates != null && userGates.size() > 0) {
			String userId = SysLoginUtil.getLoginUserId();
			for (UserGate userGate : userGates) {
				userGate.setRecId(StringUtils.uuid());
				userGate.setInsertStaffId(userId);
				userGate.setInsertTime(new Date());
				userGate.setLastModStaffId(userId);
				userGate.setLastModTime(userGate.getInsertTime());
			}
			userGateDao.insertBatch(userGates);
		}
	}
	
	/**
	 * 批量删除
	 * @param userId
	 * @param gateLinkIds
	 */
	private void deleteBatch(String userId, List<String> gateLinkIds) {
		if (gateLinkIds == null || gateLinkIds.size() == 0) return;
		Map<String, Object> params = new HashMap<>();
		params.put("userId", userId);
		params.put("list", gateLinkIds);
		userGateDao.deleteByUserId(params);
	}
	
}
